import random
import time

from proto.stream import stream_pb2, stream_pb2_grpc
import grpc

# request = user_pb2.HelloRequest()
#
# request.name = "SongYu"
#
# res_str = request.SerializeToString()
# print(res_str)
#
#
# request2 = user_pb2.HelloRequest()
# request2.ParseFromString(res_str)
# print(request2.name)

def service_stream():
    with grpc.insecure_channel("localhost:5001") as ch:
        stub = stream_pb2_grpc.GreeterStub(ch)
        # rsp: user_pb2.HelloReply = stub.SayHelloAgain(user_pb2.HelloRequest(name="SongYu"))
        # print(rsp.message)
        res = stub.GetStream(stream_pb2.StreamReqData(data="SongYu"))
        for item in res:
            print(item.data)


def get_data():

    while 1:
        time.sleep(1)
        data = str(random.random())
        yield stream_pb2.StreamReqData(data=data)


def client_stream():
    with grpc.insecure_channel("localhost:5001") as ch:
        stub = stream_pb2_grpc.GreeterStub(ch)
        # rsp: user_pb2.HelloReply = stub.SayHelloAgain(user_pb2.HelloRequest(name="SongYu"))
        # print(rsp.message)
        res = stub.PostGetStream(get_data())
        print(res.data)


def all_stream():
    with grpc.insecure_channel("localhost:5001") as ch:
        stub = stream_pb2_grpc.GreeterStub(ch)
        # rsp: user_pb2.HelloReply = stub.SayHelloAgain(user_pb2.HelloRequest(name="SongYu"))
        # print(rsp.message)
        res = stub.AllGetStream(get_data())
        for item in res:
            print(item.data)

if __name__ == '__main__':
    all_stream()